package com.lrkj.payfee.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.lrkj.common.annotation.Log;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.domain.entity.SysUser;
import com.lrkj.common.core.page.TableDataInfo;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.payfee.domain.ComputePayFeeLog;
import com.lrkj.payfee.domain.Receipt;
import com.lrkj.payfee.domain.RuleCost;
import com.lrkj.payfee.domain.TollCenter;
import com.lrkj.payfee.domain.vo.RoomNormVo;
import com.lrkj.payfee.service.IComputePayFeeLogService;
import com.lrkj.payfee.service.IRuleCostService;
import com.lrkj.payfee.service.ITollCenterService;
import com.lrkj.payfee.vo.ComputePayFeeLogVo;
import com.lrkj.payfee.vo.NormChargeVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * 收费中心
 **/
@RestController
@RequestMapping("/payfee/tollCenter")
public class TollCenterController extends BaseController {

    @Autowired
    private ITollCenterService tollCenterService;

    @Autowired
    private IRuleCostService ruleCostService;

    @Autowired
    private IComputePayFeeLogService computePayFeeLogService;

    /**
     * 创建房屋费用单
     */
    @ApiOperation(value = "创建房屋费用单", notes = "创建房屋费用单")
    @Log(title = "创建房屋费用单", businessType = BusinessType.INSERT)
    @PostMapping("/createRoomCost")
    public AjaxResult createRuleCost(@RequestBody RoomNormVo roomNormVo) {
        return toAjax(tollCenterService.createRoomCost(roomNormVo));
    }


    /**
     * 逐条创建房屋费用单
     */
    @ApiOperation(value = "逐条创建房屋费用单", notes = "逐条创建房屋费用单")
    @Log(title = "逐条创建房屋费用单", businessType = BusinessType.INSERT)
//    @PreAuthorize("@ss.hasPermi('payfee:tollCenter:singleCreateRoomCost')")
    @PostMapping("/singleCreateRoomCost")
    public AjaxResult singleCreateRoomCost(@RequestBody TollCenter tollCenter) throws ParseException {
        return AjaxResult.success(tollCenterService.singleCreateRoomCost(tollCenter));
    }


    /**
     * 房屋收费
     */
    @ApiOperation(value = "房屋收费", notes = "房屋收费")
    @Log(title = "房屋收费", businessType = BusinessType.INSERT)
    @PostMapping("/roomCharge")
    public AjaxResult roomCharge(@RequestBody Receipt receipt) {
        return AjaxResult.success(tollCenterService.roomCharge(receipt));
    }


    /**
     * 结账检查生成当期未生成的常规费用单
     */
    @ApiOperation(value = "结账检查生成当期未生成的常规费用单", notes = "结账检查生成当期未生成的常规费用单")
    @Log(title = "结账检查生成当期未生成的常规费用单", businessType = BusinessType.INSERT)
    @PostMapping("/createFeeForm")
    public AjaxResult createFeeForm(@RequestBody NormChargeVo normChargeVo) {
        if (StringUtils.isEmpty(normChargeVo.getFiscalPeriod())) {
            throw new BaseException("当前账期不能为空,请刷新重新尝试。");
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        ThreadUtil.execAsync(() -> {
            tollCenterService.createFeeForm(normChargeVo, user);
        });
        return AjaxResult.success();
    }

    /***
     *
     * 查询账期内生成的账单
     *
     * */
    @ApiOperation(value = "查询账期内生成的账单", notes = "查询账期内生成的账单")
    @GetMapping("/selectFeeFormByFiscalPeriod")
    public TableDataInfo selectFeeFormByFiscalPeriod(NormChargeVo normChargeVo) {
        startPage();
        List<RuleCost> list = ruleCostService.selectFeeFormByFiscalPeriodList(normChargeVo);
        return getDataTable(list);
    }


    @ApiOperation(value = "费用管理计算费用", notes = "费用管理计算费用")
    @Log(title = "费用管理计算费用", businessType = BusinessType.INSERT)
    @PostMapping("/computePayfee")
    public AjaxResult computePayfee(@RequestBody ComputePayFeeLog computePayFee) {
        return toAjax(tollCenterService.computePayfee(computePayFee));
    }


    /***
     *
     * 查询计费日志
     *
     * */
    @ApiOperation(value = "查询计费日志", notes = "查询计费日志")
    @GetMapping("/selectLogsIds/{ids}")
    public TableDataInfo selectComputePayFeeLog(@PathVariable String[] ids) {
        startPage();
        List<ComputePayFeeLogVo> list = computePayFeeLogService.listByIds(ids);
        return getDataTable(list);
    }

}
